<?php
require_once dirname(__FILE__).'/classThumb.class.php';
/* **************** *******************************
* Author :		Pradeep gupta
* Date :			July 2005
* function:		Create thumb of the image.
* Parameters:
*		$path	:---- This is path of the image for which we have to create the 							  thumb.[Relative path for the source file]
*		$size	:---- Size of the thumb.[Example: 100].
*		$save_path:---Path where thumbnail will be stored.[Relative path for the 							  destination file]
		$thumbType:---Type of the thumb [ this is an optional parameter and you can leave it blank ]
*				H : pass this if you want to create thumb of the fixed height $size
				W : pass this if you want to create thumb of the fixed width $size
				If passed nothing :-------- it will create a thumb of given size by automatically calculating the height and width corresponding to to the $size.
*****************************************************/



function create_thumb($path, $size, $save_path, $thumbType='')
{
	//echo "sdsd";
	//echo $path;
	if (file_exists($path))
	{
		$thumb=new my_thumbnail($path, $save_path);	// generate image_file, set filename to resize
		$thumb->size_width(500);		// set width for thumbnail, or
		$thumb->size_height(500);		// set height for thumbnail, or

		$width=$thumb->img["width"];	//origional weight of the image
		$height=$thumb->img["height"];	//origional height of the image
		if($width>$size || $height>$size)
		{
			$size=$size;
		}
		else
		{
			$size=$width;
		}

		/*	THIS FUNCTION IS USED TO CREATE FIXED THUMB OF FIXED HEIGHT */
		if( $thumbType=='H' ){
			$thumb->size_auto_width($size);// set the biggest width or height for thumbnail
		}
		/*	THIS FUNCTION IS USED TO CREATE FIXED THUMB OF FIXED WIDTH	*/
		elseif( $thumbType=='W' ){
			$thumb->size_auto_height($size);// set the biggest width or height for thumbnail
		}
		/*	THIS FUNCTION IS USED TO CREATE THUMB	*/
		else{
			$thumb->size_auto($size);// set the biggest width or height for thumbnail
		}

		$thumb->jpeg_quality(100);		// [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
	
//		$thumb->show();					// show your thumbnail
		$thumb->save($save_path);		// save your thumbnail to file
//				echo "saved";
	}
	else
	{
				return false;
	}
}
/*----------------------------------------------*/

/* **************** *******************************
* Author :		Pradeep gupta
* Date	:		July 2005
* function:		returns img tag with the path of the image.( src='imagepath' width='' height='' )
* Parameters:
*		$imgPrefix: Prefix of the thumb image like(thumb_,desc_)
		$imgName: Name of the origional image 
		$relativePath: stores relative path of the folder where image resides.example: 				  "../product_images/"
		$maxHeight	:	If thumbnail not exists then what will be the height of the image
		$maxWidth	:If thumbnail not exists then what will be the width of the image
		$mainImageFolder:	Relative Path of the origional image.
***************************************************/

function imageSrc($imgPrefix, $imgName, $relativePath, $maxHeight, $maxWidth, $mainImageFolder=''){
	if(trim($imgName)!="")
	 {		
		   $imagePath =	$relativePath . $imgPrefix . $imgName;
		   if( !is_file($imagePath) )	
			 {	  //IF IMAGE NOT FOUND :: SHOW THE IMAGE BY CALCULATING RELATIVE HEIGHT -WIDTH
				  $relativePath = $mainImageFolder=='' ? $relativePath : $mainImageFolder;
				  $imagePath = $relativePath .$imgName;
				  if( !is_file($imagePath) ){
					  return '';						  
				  }
				  $imgSrc = "src='".$imagePath."' ".CheckImageSizeimageSrc($imagePath, $maxWidth, $maxHeight);
				  return $imgSrc;
			 }
		   $imgSrc = "src='".$imagePath."'";
		   return($imgSrc);	  
		  
	 } 
	 return '';
}	//	END OF FUNCTION imageSrc


/*********************************************************
*	modified by : pradeep
*	15th of april 2005
*	returns the relative height and width of an image
*********************************************************/
function CheckImageSizeimageSrc($imageUrl, $imMaxWidth, $imMaxHeight)
{
	$img_dim = array();
	if((file_exists($imageUrl)))
	{
		if($imDetail=getimagesize($imageUrl))
		{
			//print_r($imDetail);
			$iWidth = $imDetail[0];
			$iHeight = $imDetail[1];
			$toWidth=$iWidth;
			$toHeight=$iHeight;
			//echo $iWidth." ".$iHeight." ".$imMaxWidth;
			if(($iWidth>=$iHeight) && ($iWidth>$imMaxWidth))
			{
				$newWidth=$imMaxWidth;
				$reduce_per=($imMaxWidth/$iWidth)*100;
				$newHeight=($iHeight*$reduce_per)/100;
			}
			else if(($iWidth<$iHeight) && ($iHeight>$imMaxHeight))
			{
				$newHeight=$imMaxHeight;
				$reduce_per=($imMaxHeight/$iHeight)*100;
				$newWidth=($iWidth*$reduce_per)/100;
			}
			else
			{
				$newHeight=$iHeight;
				$newWidth=$iWidth;
			}
			$img_dim['width'] = round($newWidth);
			$img_dim['height'] = round($newHeight);
		}
		else
		{
			$img_dim['width'] = "";
			$img_dim['height'] = "";
		}

		$idimension = " width = '$img_dim[width]' height='$img_dim[height]'";
	}
	return $idimension;
}	//	END OF FUNCTION CheckImageSizeimageSrc
?>